package com.dan.inter.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dan.inter.service.GaoDeMapService;
import com.dan.inter.util.HttpUtils;

@Service
public class GaoDeMapServiceImpl implements GaoDeMapService {
	
	private static final Log LOG = LogFactory.getLog(GaoDeMapServiceImpl.class);

	private static final String output = "JSON";
	
	private static final String webServiceKey = "dd5af31087a2d210c0b297015aa3dd12";
	
	private static final String webPlatformKey = "aa733be269ddcad9f068ed699f6f505d";
	
	@Override
	public String getCoordinateByAddress(Map<String,String> paramMap) throws Exception {
		
//		String url = "http://restapi.amap.com/v3/geocode/geo";
		String url = paramMap.get("coordinateUrl");
		
		Map<String, String> params = new HashMap<>();
		params.put("address", paramMap.get("address"));
		params.put("output", paramMap.get("output"));
		params.put("key", paramMap.get("webServiceKey"));
		
		String response = HttpUtils.sendPost(url, params);
		JSONObject jsonObject = JSONObject.parseObject(response);
		String count = jsonObject.getString("count");
		String location = null;
		if (Integer.parseInt(count) > 0) {
			JSONArray jsonArray = jsonObject.getJSONArray("geocodes");
			JSONObject jsonArray_node = jsonArray.getJSONObject(0);
			location = jsonArray_node.get("location").toString();
		}else{
			LOG.info("无返回数据");
			throw new Exception("无返回数据");
		}
		return location;
	}

//	@Override 
//	public String pathPlanningCon(Map<String,String> paramMap) throws Exception {
//		// 拆分地址为from 和 to
//		Map<String, String> splitAddressMap = splitAddress(paramMap.get("guestRequest"));
//		String from = splitAddressMap.get("from");
//		String to = splitAddressMap.get("to");
//		String from_Location_Coordinate = getCoordinateByAddress(from);
//		String to_Location_Coordinate = getCoordinateByAddress(to);
//		
//		String pathResult = pathPlanning(from_Location_Coordinate,to_Location_Coordinate);
//		return pathResult;
//	}
	
	public String pathPlanningByMap(Map<String,String> map) throws Exception{
//		String url = "https://m.amap.com/navi/";
		String url = map.get("pathPlanUrl");
		
//		Map<String, String> params = new HashMap<>();
//		params.put("start", map.get("start"));
//		params.put("dest", map.get("dest"));
//		params.put("destName", map.get("destName"));
//		params.put("naviBy", map.get("naviBy"));
//		params.put("key", map.get("webPlatformKey"));
		
		String respUrl =url+"?start="+map.get("start")+"&dest="+map.get("dest")+"&destName="+map.get("destName")+"&naviBy="+map.get("naviBy")+"&key="+map.get("webPlatformKey");
		return "您需要的路径已经帮您规划好了，<a href='"+respUrl+"'>点击我哦！</a>";
	}
	
	public String pathPlanning(String from,String to) throws Exception{
		
		String url ="http://restapi.amap.com/v3/direction/walking";
		Map<String, String> params = new HashMap<>();
		params.put("output", output);
		params.put("key", webServiceKey);
		params.put("origin", from); // 出发地
		params.put("destination", to); // 目的地
		
		String response = HttpUtils.sendPost(url, params);
		
		JSONObject jsonObject = JSONObject.parseObject(response);
		String count = jsonObject.getString("count");
		String total_step = null;
		if (Integer.parseInt(count) > 0) {
			String route = jsonObject.getString("route");
			JSONObject routeJsonObject = JSONObject.parseObject(route);
			JSONArray jsonArray = routeJsonObject.getJSONArray("paths");
			
			JSONObject jsonArray_node = jsonArray.getJSONObject(0);
			JSONArray stepsJsonArray = jsonArray_node.getJSONArray("steps");
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < stepsJsonArray.size(); i++) {
				String steps = stepsJsonArray.get(i).toString();
				JSONObject stepsJsonObject = JSONObject.parseObject(steps);
				String step = stepsJsonObject.getString("instruction");
				if (i<stepsJsonArray.size()-1) {
					sb.append(step+",");
				}else{
					sb.append(step);
				}
			}
			total_step = sb.toString();
		}
		return total_step;
	}
	

	/*
	 * 拆分地址为from 和 to
	 */
	public Map<String,String> splitAddress(String path){
		int from = path.indexOf("从");
		int to = path.indexOf("到");
		String firstSub = path.substring(from+1, to);
		String endSub = path.substring(to+1, path.length());
		Map<String,String> splitAddressMap = new HashMap<>();
		splitAddressMap.put("from", firstSub);
		splitAddressMap.put("to", endSub);
		return splitAddressMap;
	}

	@Override
	public String userLocation(Map<String,String> paramMap) throws Exception{
		 String url = "http://m.amap.com/navi/";
		 String dest = paramMap.get("longitude")+","+paramMap.get("latitude");
		 String destName = "当前位置";
		 String respUrl = url+"?dest="+dest+"&destName="+destName+"&key="+webPlatformKey;
		 return "您的位置已经确定，<a href='"+respUrl+"'>点击我哦！</a>"+dest;
	}

	@Override
	public String pathPlanningConByMap(Map<String, String> paramMap) throws Exception {
		
		Map<String, String> CoordinateStartMap = new HashMap<>();
		CoordinateStartMap.put("address", paramMap.get("start"));
		CoordinateStartMap.put("output", paramMap.get("output"));
		CoordinateStartMap.put("webServiceKey",paramMap.get("webServiceKey"));
		CoordinateStartMap.put("coordinateUrl", paramMap.get("coordinateUrl"));
		String start_Location_Coordinate = getCoordinateByAddress(CoordinateStartMap);
		
		Map<String, String> CoordinateDesttMap = new HashMap<>();
		CoordinateDesttMap.put("address", paramMap.get("dest"));
		CoordinateDesttMap.put("output", paramMap.get("output"));
		CoordinateDesttMap.put("webServiceKey",paramMap.get("webServiceKey"));
		CoordinateDesttMap.put("coordinateUrl", paramMap.get("coordinateUrl"));
		String dest_Location_Coordinate = getCoordinateByAddress(CoordinateDesttMap);
		
		Map<String, String> map = new HashMap<>();
		map.put("start", start_Location_Coordinate);
		map.put("dest", dest_Location_Coordinate);
		map.put("destName", paramMap.get("dest"));
		map.put("naviBy", paramMap.get("naviBy"));
		map.put("webPlatformKey", paramMap.get("webPlatformKey"));
		map.put("pathPlanUrl", paramMap.get("pathPlanUrl"));
		
		String pathPlanningByMap = pathPlanningByMap(map);
		return pathPlanningByMap;
	}
	
}
